package math.inexact;

import java.math.BigDecimal;

import math.ComplexNumber;

public class Complex implements ComplexNumber {

	private BigDecimal real;
	private BigDecimal imag;

	public Complex(BigDecimal real) {
		this.real = real;
		this.imag = BigDecimal.ZERO;
	}

	public Complex(BigDecimal real, BigDecimal imag) {
		this.real = real;
		this.imag = imag;
	}

	public BigDecimal getReal() {
		return real;
	}

	public BigDecimal getImag() {
		return imag;
	}

	public Complex add(Complex other) {
		return new Complex(real.add(other.real), imag.add(other.imag));
	}

	@Override
	public String toString() {
		if (imag.equals(BigDecimal.ZERO))
			return real.toString();
		String plus = imag.compareTo(BigDecimal.ZERO) > 0 ? "+" : "";
		return real.toString() + plus + imag.toString() + "i";
	}

	@Override
	public boolean equals(Object other) {
		if (!(other instanceof Complex))
			return false;
		return real.equals(((Complex) other).real)
				&& imag.equals(((Complex) other).imag);
	}

	@Override
	public Complex toInexact() {
		return this;
	}

	@Override
	public ComplexNumber negate() {
		return new Complex(real.negate(), imag.negate());
	}

}
